package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.api.OrderProductService;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ClearCartItemRequest;
import com.cskaoyan.mall.dto.ClearCartItemResponse;
import com.cskaoyan.shopping.dal.entitys.Member;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.CartForm;
import com.cskaoyan.shopping.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: ICartController
 * @Description: TODO  购物车controller
 * @Author:zk
 * @Date: 2022/5/19 16:29
 * @Version: 1.0
 **/
@RestController
public class ICartController implements OrderProductService {
    @Autowired
    ICartService iCartService;



    @GetMapping("/shopping/carts")
    public ResponseData cartList(HttpServletRequest request){
        String token = request.getHeader("user_info");
        System.out.println(token);
        Member user = JSON.parseObject(token, Member.class);
        Long id = user.getId();

        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(id);
        CartListByIdResponse cartListByIdResponse = iCartService.getCartListById(cartListByIdRequest);

        return new ResponseUtil<>().setData(cartListByIdResponse.getCartProductDtos());
    }

    /**
     * @author: zk
     * @description: TODO 添加商品至购物车
     * @date: 2022/5/19 22:43
     */
    @PostMapping("/shopping/carts")
    public ResponseData addToCart(@RequestBody CartForm cartForm ){
        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId(cartForm.getUserId());
        addCartRequest.setItemId(cartForm.getProductId());
        addCartRequest.setNum(cartForm.getProductNum());

        AddCartResponse addCartResponse = iCartService.addToCart(addCartRequest);
        if (addCartResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(addCartResponse);
        }
        return new ResponseUtil<>().setErrorMsg(addCartResponse.getMsg());
    }

    /**
     * @author: zk
     * @description: TODO 更新购物车中的商品
     * @date: 2022/5/19 16:48
     */
    @PutMapping("/shopping/carts")
    public ResponseData updateCartProduct(@RequestBody CartForm cartForm){
        UpdateCartNumRequest updateCartNumRequest = new UpdateCartNumRequest();
        updateCartNumRequest.setUserId(cartForm.getUserId());
        updateCartNumRequest.setChecked(cartForm.getChecked());
        updateCartNumRequest.setItemId(cartForm.getProductId());
        updateCartNumRequest.setNum(cartForm.getProductNum());

        //包装request传入service进行业务处理
        UpdateCartNumResponse updateCartNumResponse = iCartService.updateCartNum(updateCartNumRequest);
        updateCartNumResponse.setMsg("成功");
        return new ResponseUtil<>().setData(updateCartNumResponse);

    }


    /**
     * @author: zk
     * @description: TODO 删除购物车中的商品
     * @date: 2022/5/19 20:47
     */
    @DeleteMapping("/shopping/carts/{uid}/{pid}")
    public ResponseData deleteCartProduct(@PathVariable("uid")Long uid,@PathVariable("pid")Long pid){
        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setUserId(uid);
        deleteCartItemRequest.setItemId(pid);

        DeleteCartItemResponse deleteCartItemResponse = iCartService.deleteCartItem(deleteCartItemRequest);
        deleteCartItemResponse.setMsg("成功");
        return new ResponseUtil<>().setData(deleteCartItemResponse);
    }

    /**
     * @author: zk
     * @description: TODO 删除购物车中选中状态的所有商品
     * @date: 2022/5/19 21:59
     */
    @DeleteMapping("/shopping/items/{uid}")
    public ResponseData deleteCheckedItem(@PathVariable("uid")Long uid){
        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(uid);

        DeleteCheckedItemResposne deleteCheckedItemResposne = iCartService.deleteCheckedItem(deleteCheckedItemRequest);
        deleteCheckedItemResposne.setMsg("成功");
        return new ResponseUtil<>().setData(deleteCheckedItemResposne);
    }

    /**
     * @author: zk
     * @description: TODO  全选购物车中所有商品
     * @date: 2022/5/19 22:24
     */
    @PutMapping("/shopping/items")
    public ResponseData checkAllItem(@RequestBody CartForm cartForm){
        CheckAllItemRequest checkAllItemRequest = new CheckAllItemRequest();
        checkAllItemRequest.setUserId(cartForm.getUserId());
        checkAllItemRequest.setChecked(cartForm.getChecked());

        CheckAllItemResponse checkAllItemResponse = iCartService.checkAllCartItem(checkAllItemRequest);
        checkAllItemResponse.setMsg("成功");
        return new ResponseUtil<>().setData(checkAllItemResponse);
    }


    @Override
    public ClearCartItemResponse clearCartItemByUserID(ClearCartItemRequest request) {
        ClearCartItemResponse clearCartItemResponse = iCartService.clearCartItemByUserID(request);
        return clearCartItemResponse;
    }
}
